/**
 * 对日期进行格式化，
 * @param date 要格式化的日期
 * @param format 进行格式化的模式字符串yyyy-MM-dd hh:mm:ss,SSS,q季度
 * @return String
 * @author yanis.wang
 * @see	http://yaniswang.com/frontend/2013/02/16/dateformat-performance/
 */
Vue.filter('formatDate', function (value, format) {
	return utils.formatDate(value, format);
});

/**
 * 对日期进行格式化，
 * @param date 要格式化的日期时间戳
 * @param format 进行格式化的模式字符串yyyy-MM-dd hh:mm:ss,SSS,q季度
 * @return String
 * @author yanis.wang
 * @see	http://yaniswang.com/frontend/2013/02/16/dateformat-performance/
 */
Vue.filter('formatTicks', function (value, format) {
	return utils.formatTicks(value, format);
});

/**
 * 对日期进行格式化，
 * @param date 要格式化的日期时间戳
 * @param format 进行格式化的模式字符串yyyy-MM-dd hh:mm:ss,SSS,q季度
 * @return String
 * @author yanis.wang
 * @see	http://yaniswang.com/frontend/2013/02/16/dateformat-performance/
 */
Vue.filter('formatUTCTicks', function (value, format) {
	return utils.formatUTCTicks(value, format);
});

Vue.filter('formatBool', function (value, yesText, noText) {
	yesText = yesText || "是";
	noText = noText || "";
	return value && (value === true || value === "true") ? yesText : noText;
});

/**
 * 对MAC地址进行格式化，
 */
Vue.filter('formatMac', function (value) {
	return utils.formatMac(value);
});

/**
 * 转换数字为人民币大写形式
 * 1409.50    ->  壹仟肆佰零玖元伍角
 * 6007.14    ->  陆仟零柒元壹角肆分
 * 1680.32    ->  壹仟陆佰捌拾元零叁角贰分 或者 壹仟陆佰捌拾元叁角贰分
 * 107000.53  ->  壹拾万柒仟元零伍角叁分 或者 壹拾万零柒仟元伍角叁分
 * 16409.02   ->  壹万陆仟肆佰零玖元零贰分
 * 325.04     ->  叁佰贰拾伍元零肆分
 * @param m 输入数字
 */
Vue.filter('formatRMB', function (value) {
	return utils.formatRMB(value);
});

/**
 * 转换文件大小为友好的可读形式
 * @param value 输入数字
 */
Vue.filter('formatSize', function (value) {
	return utils.formatSize(value);
});

Vue.component('vue-pagination', {
	data: function () {
		return {
			pagesize: this.psize,
			pageNum: this.pnum
		};
	},
	props: {
		total: {
			type: Number,
			default: 0
		},
		psize: {
			type: Number,
			default: 20
		},
		pnum: {
			type: Number,
			default: 1
		},
		pages: {
			type: Array,
			default: function () {
				return [10, 20, 50, 100, 300, 500];
			}
		},
		pagesShow: {
			type: Number,
			default: 10
		}
	},
	methods: {
		pageShowChange: function () {
			this.$emit('change', { pnum: 1, psize: this.pagesize });
		},
		gotoPage: function (pno) {
			this.$emit('change', { pnum: pno, psize: this.pagesize });
		}
	},
	computed: {
		pstart: function () {
			return (this.pnum - 1) * this.psize + 1;
		},
		pend: function () {
			return this.pnum * this.psize > this.total ? this.total : this.pnum * this.psize;
		},
		pinfo: function () {
			if (this.total <= 0) {
				return "暂无数据";
			} else {
				return '当前显示第 ' + ((this.pnum - 1) * this.psize + 1) + ' - ' + (this.pnum * this.psize > this.total ? this.total : this.pnum * this.psize) + ' 条，共 ' + this.total + ' 条';
			}
		},
		ptotal: function () {
			return Math.ceil(this.total / this.pagesize);
		},
		pagesNum: function () {
			var pageCount = this.ptotal;
			if (pageCount <= 0) {
				return [];
			}
			var _pages = [];
			var i;
			if (pageCount <= 10) {
				for (i = 1; i <= pageCount; i++) {
					_pages.push({ name: i, num: i });
				}
				return _pages;
			} else {
				if (this.pnum > 6) {
					var start = this.pnum - 5;
					var end = this.pnum + 4;
					if (end > pageCount) {
						start = pageCount - 9;
						end = pageCount;
					}

					_pages.push({ name: '...', num: start - 1 });
					for (i = start; i <= end; i++) {
						_pages.push({ name: i, num: i });
					}
					if (pageCount - this.pnum >= 5) {
						_pages.push({ name: '...', num: end + 1 });
					}
				} else {
					for (i = 1; i <= 10; i++) {
						_pages.push({ name: i, num: i });
					}
					_pages.push({ name: '...', num: 11 });
				}
				return _pages;
			}
		}
	},
	template: '<div style="text-align: right;"><span>当前显示第{{pstart}}&nbsp;-&nbsp;{{pend}}条&nbsp;&nbsp; 共&nbsp;{{total}}&nbsp;条&nbsp;&nbsp;</span>\
<a href="javascript:void(0);" @click="gotoPage(1);" title="首页" v-if="pnum>1" style="padding:0 3px"><img src="'+ globalData.baseUrl + '/theme/img/start.gif" border="0" title="首页" style="vertical-align: text-bottom;"/></a><img v-else src="' + globalData.baseUrl +'/theme/img/start_disabled.gif" border="0" title="首页"  style="vertical-align: text-bottom;padding: 0px 3px;"/>\
<a href="javascript:void(0);" @click="gotoPage(pnum-1);" title="上一页" v-if="pnum>1" style="padding:0 3px" ><img src="'+ globalData.baseUrl + '/theme/img/previous.gif" border="0" title="上一页"  style="vertical-align: text-bottom;"/></a><img v-else src="' + globalData.baseUrl +'/theme/img/previous_disabled.gif" border="0" title="上一页"  style="vertical-align: text-bottom;padding: 0px 3px;"/>\
<a href="javascript:void(0);" @click="gotoPage(p.num);" v-for="p in pagesNum" style="padding:0 3px" :style="{fontWeight:(pnum==p.num?\'bold\':\'normal\'),fontSize:(pnum==p.num?\'14px\':\'12px\')}">{{p.name}}</a>\
<a href="javascript:void(0);" @click="gotoPage(pnum+1);" title="下一页" v-if="pnum<ptotal" style="padding:0 3px" ><img src="'+ globalData.baseUrl + '/theme/img/next.gif" border="0" title="下一页"  style="vertical-align: text-bottom;"/></a><img v-else src="' + globalData.baseUrl +'/theme/img/next_disabled.gif" border="0" title="下一页"  style="vertical-align: text-bottom;padding: 0px 3px;"/>\
<a href="javascript:void(0);" @click="gotoPage(ptotal);" title="末页" v-if="pnum<ptotal" style="padding:0 3px" ><img src="'+ globalData.baseUrl + '/theme/img/end.gif" border="0" title="末页"  style="vertical-align: text-bottom;"/></a><img v-else src="' + globalData.baseUrl +'/theme/img/end_disabled.gif" border="0" title="末页"  style="vertical-align: text-bottom;padding: 0px 3px;"/>\
&nbsp;&nbsp;&nbsp; 每页显示条数:&nbsp; \
		<select name="numPerPage" class="small" @change="pageShowChange">\
			<option v-for="p in pages" :value="p" :selected="pagesize==p">{{p}}</option>\
		</select></div>'
});